home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / video / xevil-1.000 / xevil-1 / xshow.motif.C < prev   
C/C++ Source or Header  |  1994-09-18  |  5KB  |  246 lines

  1. // Program to display an image with the appropriate mask.
  2. // Steve Hardt 2/7/93
  3.  
  4.  
  5. // Include Files
  6. extern "C"
  7. {
  8. // C includes
  9. #include <stdio.h>
  10. #include <stdlib.h> // For exit().
  11. #include <time.h>
  12.  
  13. // Unix includes
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16.  
  17. // X includes
  18. #include <X11/X.h>
  19. #include <X11/Intrinsic.h>
  20. #include <X11/keysym.h>
  21. #include <Xm/Xm.h>
  22. #include <Xm/DrawingA.h>
  23. }
  24. #include <iostream.h>
  25.  
  26.  
  27. // Defines
  28. #define FILENAME_LENGTH 100
  29. #define TIME_UNIT 1000
  30.  
  31.  
  32. // Structures
  33. struct State {
  34.   State(XtAppContext,Widget,int,char **);
  35.   void reloadImage();
  36.  
  37.   XtAppContext app;
  38.   Widget drawing;
  39.   Display *dpy;
  40.   Screen *scr_ptr;
  41.   int scr_num;
  42.   Window root;
  43.   int depth;
  44.   Colormap cmap;
  45.   GC gc;
  46.  
  47.   char pixmapName[FILENAME_LENGTH];
  48.   Boolean maskSet;
  49.   char maskName[FILENAME_LENGTH];
  50. };
  51.  
  52.  
  53.  
  54. // Functions
  55. void State::reloadImage()
  56. {
  57.   unsigned int pixmapWidth,pixmapHeight,
  58.   maskWidth,maskHeight;
  59.   int x_hot,y_hot;
  60.   Pixmap pixmap,mask;
  61.  
  62.   if (XReadBitmapFile(dpy,root,pixmapName,
  63.           &pixmapWidth,&pixmapHeight,&pixmap,&x_hot,&y_hot) != 
  64.      BitmapSuccess)
  65.     {
  66.       cout << "Failed to read pixmap." << endl;
  67.       return;
  68.     }
  69.  
  70.   if (maskSet)
  71.     {
  72.       if (XReadBitmapFile(dpy,root,maskName,
  73.               &maskWidth,&maskHeight,&mask,&x_hot,&y_hot) != 
  74.       BitmapSuccess)
  75.     {
  76.       XFreePixmap(dpy,pixmap);
  77.       cout << "Failed to read mask." << endl;
  78.       return;
  79.     }
  80.  
  81.       if ((pixmapWidth != maskWidth) || (pixmapHeight != maskHeight))
  82.     {
  83.       XFreePixmap(dpy,pixmap);
  84.       XFreePixmap(dpy,mask);
  85.       cout << "Pixmap and mask are not of the same size." << endl;
  86.       return;
  87.     }
  88.  
  89.       XSetClipMask(dpy,gc,mask);
  90.     }
  91.  
  92.   XClearWindow(dpy,XtWindow(drawing));
  93.  
  94.   XtVaSetValues(drawing,
  95.         XmNwidth,pixmapWidth,
  96.         XmNheight,pixmapHeight,
  97.         NULL);
  98.   
  99.   XCopyPlane(dpy,pixmap,XtWindow(drawing),gc,
  100.          0,0,pixmapWidth,pixmapHeight,0,0,1UL);
  101.  
  102.   XSetClipMask(dpy,gc,None);
  103.   XFreePixmap(dpy,pixmap);
  104.   if (maskSet)
  105.     XFreePixmap(dpy,mask);
  106.   
  107.   if (maskSet)
  108.     cout << pixmapName << " with mask " << maskName << " reloaded." << endl;
  109.   else
  110.     cout << pixmapName << " reloaded." << endl;
  111. }
  112.  
  113.  
  114.  
  115.  State::State(XtAppContext aContext,Widget d,int argc,char **argv)
  116. {
  117.   app = aContext;
  118.   drawing = d;
  119.   dpy = XtDisplay(d);
  120.   scr_ptr = DefaultScreenOfDisplay(dpy);
  121.   scr_num = DefaultScreen(dpy);
  122.   root = RootWindowOfScreen(scr_ptr);
  123.   depth = DefaultDepthOfScreen(scr_ptr);
  124.   cmap = DefaultColormap(dpy,scr_num);
  125.  
  126.   if (argc < 4)
  127.     {
  128.       cout << "Usage: " << argv[0] << " <foreground> <background> <pixmap> " <<
  129.     "[mask]" << endl;
  130.       exit (-1);
  131.     }
  132.   
  133.   XColor actual,database;
  134.   XGCValues values;
  135.  
  136.   if (! XAllocNamedColor(dpy,cmap,argv[1],&actual,&database))
  137.     {
  138.       cout << "Could not allocate foreground color" << endl;
  139.       exit (-1);
  140.     }
  141.   values.foreground = actual.pixel;
  142.  
  143.   if (! XAllocNamedColor(dpy,cmap,argv[2],&actual,&database))
  144.     {
  145.       cout << "Could not allocate background color" << endl;
  146.       exit (-1);
  147.     }
  148.   values.background = actual.pixel;
  149.   
  150.   strcpy(pixmapName,argv[3]);
  151.   if (argc >= 5)
  152.     {
  153.       maskSet = True;
  154.       strcpy(maskName,argv[4]);
  155.     }
  156.   else
  157.     maskSet = False;
  158.  
  159.   gc = XCreateGC(dpy,root,GCForeground|GCBackground,&values);
  160. }
  161.  
  162.  
  163.  
  164. void reloadImageCB (Widget, State *state, XmDrawingAreaCallbackStruct *cbs)
  165. {
  166.   // Implement quit function.
  167.   if ((cbs->reason == XmCR_INPUT) &&
  168.       (cbs->event->xany.type == KeyPress))
  169.     {
  170.       KeySym keysym;
  171.       Modifiers dummy;
  172.       XtTranslateKeycode(state->dpy,cbs->event->xkey.keycode,
  173.              cbs->event->xkey.state,&dummy,&keysym);
  174.  
  175.       if (keysym == XK_q)
  176.     exit(0);
  177.     }
  178.  
  179.   state->reloadImage();
  180. }
  181.  
  182.  
  183.  
  184. // Reload image if either pixmap or mask has been modified 
  185. void checkImageTO(State *state,XtIntervalId)
  186. {
  187.   static Boolean already;
  188.   static time_t pixmapTime;
  189.   static time_t maskTime;
  190.  
  191.   struct stat pixmapStat;
  192.   struct stat maskStat;
  193.  
  194.   stat(state->pixmapName,&pixmapStat);
  195.   if (state->maskSet)
  196.     stat(state->maskName,&maskStat);
  197.   
  198.   if ((! already) ||
  199.       difftime(pixmapStat.st_mtime,pixmapTime) ||
  200.       (state->maskSet && difftime(maskStat.st_mtime,maskTime)))
  201.     {
  202.       pixmapTime = pixmapStat.st_mtime;
  203.       if (state->maskSet)
  204.     maskTime = maskStat.st_mtime;
  205.       state->reloadImage();
  206.       already = True;
  207.     }
  208.  
  209.   XtAppAddTimeOut(state->app,TIME_UNIT,(XtTimerCallbackProc)checkImageTO,
  210.            (XtPointer)state);
  211. }
  212.  
  213.  
  214.  
  215. main (int argc, char **argv)
  216. {
  217.   XtAppContext app;
  218.  
  219.   Widget toplevel = 
  220.     XtVaAppInitialize(&app,"xbmshow",
  221.               NULL,0,
  222.               (int *)(&argc),argv,
  223.               NULL,
  224.               XmNallowShellResize,True,
  225.               NULL);
  226.   XtVaSetValues(toplevel,
  227.         XmNtitle, "xbmshow",
  228.         NULL);
  229.  
  230.   Widget drawing = XtVaCreateManagedWidget("drawing",
  231.                        xmDrawingAreaWidgetClass, toplevel,
  232.                        NULL);
  233.   
  234.   State state(app,drawing,argc,argv);
  235.   
  236.   XtAddCallback(drawing,XmNinputCallback,(XtCallbackProc)reloadImageCB,
  237.         (XtPointer)(&state));
  238.   XtAddCallback(drawing,XmNexposeCallback,(XtCallbackProc)reloadImageCB,
  239.         (XtPointer)(&state));
  240.   XtAppAddTimeOut(app,TIME_UNIT,(XtTimerCallbackProc)checkImageTO,
  241.            (XtPointer)(&state));
  242.  
  243.   XtRealizeWidget(toplevel);
  244.   XtAppMainLoop(app);
  245. }
  246.